/**
 * Created by lyf on 2017/6/20.
 */

'use strict'
let node_ssh = require('node-ssh');
const serviceConfig = require('../conf/service.json');
let ssh = new node_ssh();

module.exports = async function deployPostgres (kong){

    return new Promise (async function(resolve,reject){
        console.log(kong);
        for ( let i = 0 ; i < kong.length ; i++){
            await ssh.connect({
                host: kong[i][1],
                username: serviceConfig.user ,
                password: serviceConfig.password
            });
            const result = await ssh.execCommand('docker run -d --restart=on-failure:5 ' +
                ' --name ab-gateway-database ' +
                ' -p 5432:5432 ' +
                ' -e "POSTGRES_USER=kong" ' +
                ' -e "POSTGRES_DB=kong" ' +
                ' -v ~/seckill/kong/postgres/data:/var/lib/postgresql/data ' +
                ' postgres:9.4 '
            );
            ssh.dispose();
            //result.stdout ? resolve() : reject ();

            console.log(`postgres:${kong[i][1]}`);
            console.log("-----------------------------");
            console.log(result.stdout);
            console.log(result.stderr);
            console.log("-----------------------------");
            resolve();

        }
    })

};